let pattern = "abba", s = "dog constructor constructor dog"

function wordPattern(pattern, s){
    let patArr = [...pattern];
    let wordArr = s.split(" ");
    let wordPat = new Map(); // 使用 Map 存储 pattern 字符到单词的映射
    let patWord = new Map(); // 使用 Map 存储单词到 pattern 字符的映射

    if(patArr.length != wordArr.length){
        return false
    }

    for (let i = 0; i < patArr.length; i++) {
        let pItem = patArr[i];
        let wItem = wordArr[i];

        if (wordPat.has(pItem)) {
            if (wordPat.get(pItem) !== wItem) {
                return false;
            }
        } else {
            wordPat.set(pItem, wItem);
        }

        if (patWord.has(wItem)) {
            if (patWord.get(wItem) !== pItem) {
                return false;
            }
        } else {
            patWord.set(wItem, pItem);
        }
    }

    return true;
}

console.log(wordPattern(pattern, s))